// pages/teacher/teaclass/teaclass.js
import {
  teacher
} from '../../../api/teacher.js'
import {
  parseTime
} from '../../../utils/datetime.js'
import {
  del
} from '../../../api/teacher.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  options: {
    styleIsolation: 'apply-shared'
  },

  /**
   * 组件的初始数据
   */
  data: {
    classList: [],
    show: false,
    showcart: false,
    refresh: false,
    page: 1, //页数
    limit: 10, //每页条数
    isLoadings: true, //判断是否有数据
  },
  lifetimes: {
    attached: function () {
      this.loadData();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    loadData() {
      let data = {
        page: this.data.page,
        limit: this.data.limit
      }
      teacher(data).then(res => {
        res.forEach(item=>{
          item.createtime=parseTime(item.createtime,'{y}/{m}/{d}')
        })   
        let classList = this.data.classList
        classList = classList.concat(res)
        this.setData({
          // classList: [...res,...this.classList],
          classList: classList,
          refresh: false,
          isLoadings: res.length >= this.data.limit
        })
      })
    },
    loadpage(){
      teacher().then(res=>{
        res.forEach(item=>{
          item.createtime=parseTime(item.createtime,'{y}/{m}/{d}')
        })   

        this.setData({
          classList:res
        })

      })
    },
    del(e) {
      var that=this;
      wx.showModal({
        title: '提示',
        content: '是否不再管理班级？',
        success(res) {
          if (res.confirm) {
            const item = e.target.dataset.index.id;
            del({
              id: item
            }).then(res => {      
              wx.showToast({
                title: '删除成功',
              })   
            })   
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
          that.loadpage() 
        },
      })

    },

    nav_bar() {
      wx.navigateTo({
        url: '../teacher/class/attend',
      })
    },
    //进入班级信息
    goClass(e) {
      const item = e.currentTarget.dataset.index;
      console.log(item);
      wx.navigateTo({
        url: '../teacher/class/detail',
        success: res => {
          res.eventChannel.emit('emitclass', item)
        },

      })
    },
    //下拉刷新
    refreshmessage() {
      this.setData({
        classList: [],
        page: 1
      })
      this.loadData();

    },
    //上拉加载
    onMoredetail() {
      if (this.data.isLoadings === false) return
      let page = this.data.page
      page++
      this.setData({
        page
      })
      this.loadData();

    },
    //班级信息跳转
    onClickShow() {
      this.setData({
        show: true
      }); 
    },

    //遮罩层
    onClickHide() {
      this.setData({
        show: false
      });
    },
    noop() {},
    addcourse() {
      wx.navigateTo({
        url: '../teacher/class/attend',
        events: {
          add: () => {
            this.setData({
              classList: [],
              page: 1
            })
            this.loadData()
          }
        },
      })
    }

  },
})